<html>
<head><meta charset="utf-8"><title>Get macro expanded snippet from `Span`? · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Get.20macro.20expanded.20snippet.20from.20.60Span.60.3F.html">Get macro expanded snippet from `Span`?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="219294231"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Get%20macro%20expanded%20snippet%20from%20%60Span%60%3F/near/219294231" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> JOE1994 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Get.20macro.20expanded.20snippet.20from.20.60Span.60.3F.html#219294231">(Dec 09 2020 at 03:30)</a>:</h4>
<p>Hello <span aria-label="crab" class="emoji emoji-1f980" role="img" title="crab">:crab:</span>, I've been using <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_span/source_map/struct.SourceMap.html#method.span_to_snippet"><code>SourceMap::span_to_snippet()</code></a> to get the source code snippet corresponding to the given <code>Span</code>.</p>
<p>When the API is used on <code>Span</code> of  items that were macro-generated, the returned snippet is from before macro expansion.<br>
(e.g. <code>Span</code> of an <code>ImplItem</code> =&gt; <code>unsafe impl Send for $owned{}</code>)</p>
<div class="codehilite"><pre><span></span><code>// Example from : https://github.com/sfackler/rust-openssl/blob/master/openssl/src/macros.rs#L157-L160
unsafe impl Send for $owned{}
unsafe impl Send for $borrowed{}
</code></pre></div>
<p>Is there a canonical way to get the <strong>macro-expanded snippet</strong> of an HIR item?<br>
Thank you for checking out this question <span aria-label="crab" class="emoji emoji-1f980" role="img" title="crab">:crab:</span></p>



<a name="219294681"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Get%20macro%20expanded%20snippet%20from%20%60Span%60%3F/near/219294681" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Get.20macro.20expanded.20snippet.20from.20.60Span.60.3F.html#219294681">(Dec 09 2020 at 03:40)</a>:</h4>
<p><span class="user-mention" data-user-id="242132">@JOE1994</span> I think you want <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_span/span_encoding/struct.Span.html#method.source_callsite"><code>source_callsite</code></a></p>



<a name="219299650"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Get%20macro%20expanded%20snippet%20from%20%60Span%60%3F/near/219299650" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> JOE1994 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Get.20macro.20expanded.20snippet.20from.20.60Span.60.3F.html#219299650">(Dec 09 2020 at 05:30)</a>:</h4>
<p>Thank you for the pointer! After trying it out, it seems <code>source_callsite</code> may not be what I was looking for.<br>
When I get a <code>Span</code> of a macro-generated <code>ImplItem</code> using <code>tcx.hir().span()</code> and call <code>SourceMap::span_to_snippet()</code> on it,<br>
I get something like <strong><code>unsafe impl&lt;T&gt; Send for $borrowed&lt;T&gt;{}</code></strong>,<br>
but what I want is <strong><code>unsafe impl&lt;T&gt; Send for STRUCTNAMEHERE</code>{}</strong>.</p>
<p>I want to replace the variable (that starts with <code>$</code>), with the actual name of the item that was inserted during macro expansion.</p>



<a name="219311578"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Get%20macro%20expanded%20snippet%20from%20%60Span%60%3F/near/219311578" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Get.20macro.20expanded.20snippet.20from.20.60Span.60.3F.html#219311578">(Dec 09 2020 at 09:09)</a>:</h4>
<p>You will need to take the HIR node and pretty-print it. You can't do this based on the <code>Span</code> alone, as the <code>Span</code> only points to source locations. In case of an expanded macro it points to the macro invocation and the part of the macro from which it was expanded. A <code>Span</code> doesn't have any knowledge about the language that is used. Using them for C code is also completely valid.</p>



<a name="219351102"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Get%20macro%20expanded%20snippet%20from%20%60Span%60%3F/near/219351102" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> JOE1994 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Get.20macro.20expanded.20snippet.20from.20.60Span.60.3F.html#219351102">(Dec 09 2020 at 15:26)</a>:</h4>
<p>Thank you for your response <span aria-label="smiley" class="emoji emoji-1f603" role="img" title="smiley">:smiley:</span>  I couldn't find an API that'll do what I want like magic, so I'm assuming that I need to refer to the <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/print/pretty/trait.PrettyPrinter.html"><code>PrettyPrinter</code></a> trait and their implementors to write a pretty-print code for my purpose.. :)</p>



<a name="219354394"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Get%20macro%20expanded%20snippet%20from%20%60Span%60%3F/near/219354394" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Get.20macro.20expanded.20snippet.20from.20.60Span.60.3F.html#219354394">(Dec 09 2020 at 15:47)</a>:</h4>
<p>I think you can use <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir_pretty/struct.State.html#method.print_impl_item"><code>rustc_hir_pretty::State::print_impl_item</code></a>.</p>



<a name="219429945"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Get%20macro%20expanded%20snippet%20from%20%60Span%60%3F/near/219429945" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> JOE1994 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Get.20macro.20expanded.20snippet.20from.20.60Span.60.3F.html#219429945">(Dec 10 2020 at 04:05)</a>:</h4>
<p>Thank you so much for your help ! <span aria-label="crab" class="emoji emoji-1f980" role="img" title="crab">:crab:</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>